package com.wwwzh.himalayafm.adapter.recommend;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.entity.recommend.RecommFousImage;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * Created by weizz on 2016/12/5.
 */

public class FocusImageAdapter extends PagerAdapter {
    private List<RecommFousImage> list;
    private LinkedList<View> mViewCache = null;
    private Context context;
    private LayoutInflater inflater;

    public FocusImageAdapter(List<RecommFousImage> list, Context context) {
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        this.mViewCache = new LinkedList<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewHolder viewHolder = null;
        View convertView = null;
        if(mViewCache.size() == 0){
            convertView = inflater.inflate(R.layout.layout_viewpager , null ,false);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.viewPager_image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            viewHolder = new ViewHolder();
            viewHolder.imageView = imageView;
            convertView.setTag(viewHolder);
        }else {
            convertView = mViewCache.removeFirst();
            viewHolder = (ViewHolder)convertView.getTag();
        }
//        viewHolder.setText(datas.get(position).title);
        Picasso.with(container.getContext()).load(list.get(position).getPic())
                    .into(viewHolder.imageView);
        container.addView(convertView ,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT );
        return convertView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        View contentView = (View) object;
        container.removeView(contentView);
        this.mViewCache.add(contentView);

    }
    public class ViewHolder{
        private ImageView imageView;
    }

}
